<template>
    <el-form inline="true" :model="loginForm" label-width="auto">
        <el-form-item label="账号">
            <el-input v-model="loginForm.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="loginForm.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="cleanSubmit">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { loginApi } from '@/api/login'
import router from '@/router'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'

const login = async () => {
    let result = await loginApi(loginForm.value)
    if (result.code == 200) {
        ElMessage.success('登录成功！')
        localStorage.setItem('loginUser', JSON.stringify(result.data))
        router.push('/')
    }
}

const cleanSubmit = () => {
    loginForm.value = {
        username: '',
        password: ''
    }
}


const loginForm = ref({
    username: '',
    password: ''
})
</script>

<style scoped>
.el-form {
    margin: 0 auto;
}
</style>